

var express = require('express');

var router = express.Router()


var { Resident, Advice,Cars,users,staff,Salary } = require('../utils/models');

var { createToken, checkToken } = require('../utils/token')


router.get('/test', (req, res) => {
    res.send('5165661655')
})


router.get('/gettoken', (req, res) => {
    res.json({
        code: 200,
        type: 1,
        token: '566565',
        msg: '获取假token'
    })
})

//注册逻辑
router.post('/regis', (req, res) => {
    var { mobile, password, username } = req.body;
    Resident.findOne({
        mobile,
    })
        .then(data => {
            if (data) {
                res.json({
                    code: 200,
                    type: 0,
                    msg: '手机号已被注册',
                })
            } else {
                Resident.insertMany({
                    mobile,
                    password,
                    time: new Date(),
                    roleid: '1',
                    username
                })
                    .then(result => {
                        res.json({
                            code: 200,
                            type: 1,
                            msg: '注册成功',
                            result
                        })
                    })
                    .catch(err => {
                        res.json({
                            code: 500,
                            type: 0,
                            msg: '服务器错误',
                            err
                        })
                    })
            }
        })
})


//登录存token
router.post('/login', (req, res) => {
    var { mobile, password } = req.body;
    Resident.findOne({
        mobile,
    })
        .then(result => {
            if (result) {
                if (result.password == password) {
                    var token = createToken({
                        mobile,
                        username: result.username,
                        roleid: result.roleid,
                        password
                    })
                    res.json({
                        code: 200,
                        type: 1,
                        msg: '登录成功',
                        token,
                        roleid: result.roleid,
                    })
                } else {
                    res.json({
                        code: 200,
                        type: 0,
                        msg: '手机号或密码不正确',
                    })
                }
            } else {
                res.json({
                    code: 200,
                    type: 0,
                    msg: '手机号未注册'
                })
            }
        })
})

//上传头像
//multer
var multer = require('multer');
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './public/avatar')  //文件路径
    },
    filename: function (req, file, cb) {
        cb(null, Date.now() + 'wh2004' + file.originalname)
    }
})
var upload = multer({ storage: storage }).any();//可以上传任意格式的图片

router.post("/uploadpic", upload, (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        if (req.files) {
            var avatar = req.files[0].path;//头像的服务器地址
            Resident.updateMany({
                mobile,
            }, {
                $set: {
                    avatar: avatar
                }
            }).then(result => {
                res.json({
                    code: 200,
                    msg: "头像上传成功",
                    type: 1,
                    avatar
                })
            }).catch(err => {
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    type: 0,
                    err
                })
            })
        }
        else {
            res.json({
                code: 200,
                msg: "头像上传失败",
                type: 0
            })
        }
    })

})


//获取用户信息
router.post('/getuserinfo', (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        Resident.findOne({ mobile })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    msg: '查询个人信息成功',
                    result
                })
            })
    })
})


//添加用户
router.post('/adduser', (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        var body = { ...req.body, time: new Date() }
        Resident.findOne({
            mobile: req.body.mobile
        })
            .then(data => {
                if (data) {
                    res.json({
                        code: 200,
                        type: 0,
                        msg: '手机号已被注册'
                    })
                } else {
                    Resident.insertMany(body)
                        .then(result => {
                            res.json({
                                code: 200,
                                type: 1,
                                msg: '新增用户成功'
                            })
                        })
                        .catch(err => {
                            res.json({
                                code: 500,
                                type: 0,
                                msg: '服务器异常'
                            })
                        })
                }
            })
    })
})

//查询所有用户的信息
router.post('/alluserinfo', (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        Resident.find()
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    msg: '所有的用户信息获取成功',
                    result
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    type: 0,
                    msg: '服务器异常'
                })
            })
    })
})


//删除一个用户
router.post('/deloneuser', (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Resident.remove({
            _id: req.body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    msg: '删除成功',
                    result
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    type: 0,
                    msg: '服务器异常',
                    err
                })
            })
    })
})

//获取首页banner的图片
router.post('/gethomebanner', (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        res.json({
            code: 200,
            type: 1,
            msg: '获取banner图成功',
            result: [
                {
                    imgUrl: 'https://files.porsche.cn/filestore/image/multimedia/none/971-g2-2nd-panamera-teaser/preview/979f73b0-cc01-11ea-80cb-005056bbdc38;sJ;twebp/porsche-preview.webp',
                    text: 'Panamera 10 Years Edition'
                },
                {
                    imgUrl: 'https://files.porsche.cn/filestore/image/multimedia/none/carrange-flyout-911/small/3cf76e8c-6694-11e9-80c4-005056bbdc38;sO;twebp;c1693;gc/porsche-small.webp',
                    text: '保时捷911'
                },
                {
                    imgUrl: 'https://files.porsche.cn/filestore/image/multimedia/none/carrange-flyout-taycan/small/094d1c1c-bab0-11e9-80c4-005056bbdc38;sO;twebp;c1693;gc/porsche-small.webp',
                    text: 'Taycan'
                },
                {
                    imgUrl: 'https://files.porsche.cn/filestore/image/multimedia/none/carrange-flyout-panamera/small/31077aa2-d337-11ea-80cc-005056bbdc38;sO;twebp;c1693;gc/porsche-small.webp',
                    text: 'Panamera'
                },
                {
                    imgUrl: 'https://files.porsche.cn/filestore/image/multimedia/none/carrange-flyout-718/small/ccc805c0-6693-11e9-80c4-005056bbdc38;sO;twebp;c1693;gc/porsche-small.webp',
                    text: '保时捷718'
                },
                {
                    imgUrl: 'https://files.porsche.cn/filestore/image/multimedia/none/carrange-flyout-macan/small/46131fe4-6694-11e9-80c4-005056bbdc38;sO;twebp;c1693;gc/porsche-small.webp',
                    text: 'Macan'
                },
            ]
        })
    })
})



//编辑用户信息
router.post('/edituser', (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        var body = {
            ...req.body,
            // time:new Date()
        }
        console.log(body);
        Resident.updateMany({
            _id: req.body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    tyoe: 1,
                    result,
                    msg: '修改成功'
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    tyoe: 0,
                    err,
                    msg: '服务器异常'
                })
            })
    })
})


//把用户提交的意见数据添加进意见表里
router.post('/subadvice', (req, res) => {
    checkToken(req, res, ({ mobile, roleid, username }) => {
        var body = {
            ...req.body,
            time: new Date(), //发表时间
            roleid,  //登录用户发表意见的id
            username,
            mobile
        }
        Advice.insertMany({ ...body })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    result,
                    msg: '意见发表成功'
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    tyoe: 0,
                    err,
                    msg: '服务器异常'
                })
            })
    })
})


//获取所有的意见反馈，来渲染页面
router.post('/getalladvice', (req, res) => {
    checkToken(req, res, ({ mobile, roleid }) => {
        Advice.find()
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    result,
                    msg: '意见列表信息获取成功'
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    tyoe: 0,
                    err,
                    msg: '服务器异常'
                })
            })
    })
})


//删除意见
router.post('/deloneadvice', (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        Advice.remove({
            _id: req.body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    result,
                    msg: '删除意见成功'
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    tyoe: 0,
                    err,
                    msg: '服务器异常'
                })
            })
    })
})

//获取用户想要修改的意见信息
router.post('/getadvicebyid', (req, res) => [
    checkToken(req, res, ({ mobile }) => {
        console.log(req.body);
        Advice.findOne({
            _id: req.body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    type: 1,
                    result,
                    msg: '获取意见信息成功'
                })
            })
            .catch(err => {
                res.json({
                    code: 500,
                    tyoe: 0,
                    err,
                    msg: '服务器异常'
                })
            })
    })
])

// 修改意见
router.post('/updateadvicebyid', (req, res) => {
    checkToken(req, res, ({ mobile }) => {
        var body = {
            ...req.body,
        }
        var obj = {
            _id: req.body._id
        }
        Advice.updateMany(obj, {
            $set: body
        }).then(result => {
            res.json({
                code: 200,
                type: 1,
                result,
                msg: '修改意见信息成功'
            })
        })
            .catch(err => {
                res.json({
                    code: 500,
                    tyoe: 0,
                    err,
                    msg: '服务器异常'
                })
            })
    })
})


// 回复意见
router.post('/replyadvice', (req, res) => {
    checkToken(req, res, ({ mobile, username, roleid }) => {
        var obj = {
            _id: req.body._id
        }
        Advice.findOne(obj)
            .then(data => {
                var reply = data.reply || [];
                reply.push({
                    author: username,
                    mobile,
                    roleid,
                    tips:req.body.tips,
                    time: new Date(),
                    id: reply.length
                })

                Advice.updateMany(obj, {
                    $set: { reply }
                })
                    .then(result => {
                        res.json({
                            code: 200,
                            result,
                            type: 1,
                            msg: "回复成功",
                        })
                    }).catch(err => {
                        res.json({
                            code: 500,
                            err,
                            type: 0,
                            msg: "服务器异常",
                        })
                    })
            })
    })
})

//获取车辆列表数据
router.post('/getallcarinfo',(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        var obj = {}
        var limit = req.body.limit || 0;
        Cars.find(obj,{})
        .limit(limit)
        .sort({_id:-1})
        .then(result => {
            res.json({
                code: 200,
                result,
                type: 1,
                msg: "获取车辆列表信息成功",
            })
        }).catch(err => {
            res.json({
                code: 500,
                err,
                type: 0,
                msg: "服务器异常",
            })
        })
    })
})

//车辆信息分类查询
router.post('/getcarstype',(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Cars.distinct('type')
        .then(result => {
            res.json({
                code: 200,
                result,
                type: 1,
                msg: "获取车辆列表信息成功",
            })
        }).catch(err => {
            res.json({
                code: 500,
                err,
                type: 0,
                msg: "服务器异常",
            })
        })
    })
})





//薪资列表，模糊查询
// router.post('/getsalary',(req,res)=>{
//     checkToken(req,res,({mobile,roleid})=>{
//         Salary.find({
//             roleid:/3|4|5/
//         })
//         .then(result => {
//             res.json({
//                 code: 200,
//                 result,
//                 type: 1,
//                 msg: "获取薪资列表信息成功",
//             })
//         }).catch(err => {
//             res.json({
//                 code: 500,
//                 err,
//                 type: 0,
//                 msg: "服务器异常",
//             })
//         })
//     })
// })
router.post('/getsalary',(req,res)=>{
    checkToken(req,res,({mobile,roleid})=>{
        Salary.find()
        .then(result => {
            res.json({
                code: 200,
                result,
                type: 1,
                msg: "获取薪资列表信息成功",
            })
        }).catch(err => {
            res.json({
                code: 500,
                err,
                type: 0,
                msg: "服务器异常",
            })
        })
    })
})


//添加岗位信息
router.post('/addwork',(req,res)=>{
    checkToken(req,res,({mobile})=>{
        var body = {
            ...req.body,
            time:new Date()
        }
        Salary.insertMany(body)
        .then(result => {
            res.json({
                code: 200,
                result,
                type: 1,
                msg: "添加岗位成功",
            })
        }).catch(err => {
            res.json({
                code: 500,
                err,
                type: 0,
                msg: "服务器异常",
            })
        })
    })
})


//根据用户传来的_id  删除岗位
router.post('/delworkone',(req,res)=>{
    checkToken(req,res,({mobile})=>{
        Salary.remove({
            _id:req.body._id
        })
        .then(result => {
            res.json({
                code: 200,
                result,
                type: 1,
                msg: "删除岗位成功",
            })
        }).catch(err => {
            res.json({
                code: 500,
                err,
                type: 0,
                msg: "服务器异常",
            })
        })
    })
})


//编辑岗位的信息,用户会传来_id
router.post('/updatework',(req,res)=>{
    checkToken(req,res,({mobile})=>{
        var body = {
            ...req.body
        }
        console.log(body);
        Salary.updateMany({
            _id:req.body._id
        },{
            $set:body
        })
        .then(result => {
            res.json({
                code: 200,
                result,
                type: 1,
                msg: "修改岗位成功",
            })
        }).catch(err => {
            res.json({
                code: 500,
                err,
                type: 0,
                msg: "服务器异常",
            })
        })
    })
})


module.exports = router;